//
//  SkillViewController.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/6.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "SkillViewController.h"
#import "SkillTableViewCell.h"
#import "SkillInfoViewController.h"
#import "SkillView.h"
#import "SkillHandle.h"
#import "SkillModel.h"
#import <MJRefresh.h>
#import <AVOSCloud.h>
#import "CoreDataManager.h"
@interface SkillViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)SkillView *skillView;
@property (nonatomic,assign)NSInteger npcCount;//用来上拉加载传入数据使用
@end

@implementation SkillViewController
- (void)loadView
{
    self.skillView = [[SkillView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.skillView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.npcCount = 0;
    //注册重用标志
    [self.skillView.skillTableView registerClass:[SkillTableViewCell class] forCellReuseIdentifier:@"SkillTableViewCell"];
    //遵守协议
    self.skillView.skillTableView.dataSource = self;
    self.skillView.skillTableView.delegate = self;
    //下拉刷新
    [self updateData];
    //上拉加载
    [self addData];
}

#pragma mark - 加载数据
- (void)loadData
{
    __weak typeof(self) weakself = self;
    [[SkillHandle shareSkillHandle] loadDataWithNumber:0 withBlock:^(BOOL isLoading) {
        if (isLoading) {
            [weakself.skillView.skillTableView reloadData];
        }else{
            NSLog(@"数据加载失败");
        }
    }];
}
#pragma mark - 下拉刷新
- (void)updateData
{
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullUpLoadData)];
    self.skillView.skillTableView.mj_header = header;
    [self.skillView.skillTableView.mj_header beginRefreshing];

}
#pragma mark - 下拉刷新方法
- (void)pullUpLoadData
{
    //重新加载数据
    [SkillHandle shareSkillHandle].skillDataArray = nil;
    __weak typeof(self) weakself = self;
    [[SkillHandle shareSkillHandle] loadDataWithNumber:0 withBlock:^(BOOL isLoading) {
        if (isLoading) {
            [weakself.skillView.skillTableView reloadData];
        }else{
            NSLog(@"数据加载失败");
        }
        [self.skillView.skillTableView.mj_header endRefreshing];
    }];
}
#pragma mark - 上拉加载
- (void)addData
{
    self.skillView.skillTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.npcCount++;
        __weak typeof(self) weakself = self;
        [[SkillHandle shareSkillHandle] loadDataWithNumber:self.npcCount withBlock:^(BOOL isLoading) {
            if (isLoading) {
                [weakself.skillView.skillTableView reloadData];
            }else{
                NSLog(@"数据加载失败");
            }
            [self.skillView.skillTableView.mj_footer endRefreshing];
        }];
    }];

}
#pragma mark - tableView DataSource方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [SkillHandle shareSkillHandle].skillDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SkillTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SkillTableViewCell"];
    SkillModel *model = [SkillHandle shareSkillHandle].skillDataArray[indexPath.row];
    if (cell != nil) {
        cell = [[SkillTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SkillTableViewCell"];
    }
    NSString *string = model.imageArray.firstObject;
    
    if (model.imageArray.count == 1 && string.length < 1) {
        [cell addFirstStyleCell];
        [cell getDataFromSkillModel:model];
        if (model.title.length > 24) {
            cell.titleLabel.frame = CGRectMake(10 * LAYOUT_SIZE, 10 * LAYOUT_SIZE_HEIGHT, SCREEN_WIDTH - (20 * LAYOUT_SIZE), 50 * LAYOUT_SIZE_HEIGHT);
        }
    }else if (model.imageArray.count == 1) {
        [cell addSecondSytleCell];
        [cell getDataTwoFromSkillModel:model];
        if (model.title.length > 16) {
            cell.titleLabel.frame = CGRectMake(10 * LAYOUT_SIZE, 10 * LAYOUT_SIZE_HEIGHT, 230 * LAYOUT_SIZE, 50 * LAYOUT_SIZE_HEIGHT);
        }
    }else if (model.imageArray.count > 1) {
        [cell addThirdStyleCell];
        [cell getDataThreeFromSkillModel:model];
    }
//    初次加载从LeanCloud里面拿出阅读人数赋值到cell上
    NSString *readCount = [NSString stringWithFormat:@"readCount%ld",indexPath.row];
    AVQuery *query = [AVQuery queryWithClassName:@"BasketballSkill"];
    [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        NSInteger number = [[object objectForKey:readCount]integerValue];
        NSInteger num = [(model.readarts) integerValue];
        cell.peoplesLabel.text = [NSString stringWithFormat:@"%ld",num+number];
    }];
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
#pragma mark - cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SkillModel *model = [SkillHandle shareSkillHandle].skillDataArray[indexPath.row];
     NSString *string = model.imageArray.firstObject;
    if (model.imageArray.count == 1 && string.length < 1) {
        return 100 * LAYOUT_SIZE_HEIGHT;
    }else if (model.imageArray.count == 1){
        return 130 * LAYOUT_SIZE_HEIGHT;
    }else if (model.imageArray.count >1){
        return 190 * LAYOUT_SIZE_HEIGHT;
    }
    return 100 * LAYOUT_SIZE_HEIGHT;
}
#pragma mark - cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消选中
    SkillTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    SkillModel *model = [SkillHandle shareSkillHandle].skillDataArray[indexPath.row];
    SkillInfoViewController *skillInfoVC = [[SkillInfoViewController alloc]init];
    //每次点击cell进入详情界面 都会让针对的cell对应的阅读数加1 
    AVObject *object = [AVObject objectWithoutDataWithClassName:@"BasketballSkill" objectId:@"5694625d60b24909ebc673d2"];
    object.fetchWhenSave = YES;
    NSString *readCount = [NSString stringWithFormat:@"readCount%ld",indexPath.row];
    [object setObject:[NSNumber numberWithInteger:1] forKey:readCount];
    [object incrementKey:readCount];
    [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            //不是初次加载阅读的时候走这个地方的方法
            model.readCount = [[object objectForKey:readCount]integerValue];
            NSInteger num = [(model.readarts) integerValue];
            cell.peoplesLabel.text = [NSString stringWithFormat:@"%ld",num+model.readCount];
        }else{
            NSLog(@"%@",error);
        }
    }];
    skillInfoVC.webViewUrl = model.url;
    skillInfoVC.webTitle = model.title;
    skillInfoVC.passValueId = model.iD;
    UINavigationController *skillNC = [[UINavigationController alloc]initWithRootViewController:skillInfoVC];
    skillInfoVC.modalPresentationStyle = 1;
    skillInfoVC.modalTransitionStyle = 1;
    [self presentViewController:skillNC animated:YES completion:nil];
    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [SkillHandle shareSkillHandle].skillDataArray = nil;
}
@end
